package ysq.xyg.sale.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.sale.dao.SaleProductDao;
import ysq.xyg.sale.model.po.*;
import ysq.xyg.sale.model.vo.SaleColumnVo;
import ysq.xyg.sale.service.*;
import ysq.xyg.sale.util.SaleBaseContent;
import ysq.xyg.sys.model.po.SysDepart;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sale.model.vo.SaleProductVo;
import ysq.xyg.sys.model.vo.SysDepartVo;
import ysq.xyg.sys.model.vo.SysUserVo;
import ysq.xyg.sys.service.SysUserService;

import javax.annotation.Resource;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 销售模块-销售产品service实现
 *
 * @author shengQiang yu
 * @date 2019-9-9 14:17:04
 */
@Service("saleProductService")
public class SaleProductServiceImpl extends BaseServiceImpl<SaleProduct> implements SaleProductService {

    @Resource
    private SaleProductDao saleProductDao;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SaleDetailService saleDetailService;
    @Resource
    private SaleRecordService saleRecordService;
    @Resource
    private SaleProductMyService saleProductMyService;
    @Resource
    private SaleColumnService saleColumnService;
    private SaleProduct saleProduct;

    @Autowired
    public void setBaseDao(SaleProductDao saleProductDao) {
        this.baseDao = saleProductDao;
    }

    /**
     * 创建产品
     *
     * @param id 商品id
     * @param priceType 套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
     * @param currentUser 当前用户
     * @param request
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnEntity createProduct(HttpServletRequest request, String id, String priceType, SysUser currentUser) {
        SaleProduct saleProduct=selectByPrimaryKey(id);
        if(saleProduct==null){
            return new ReturnEntity(BaseConstant.FAIL,"id错误");
        }
        SysUser sysUser=sysUserService.selectByPrimaryKey(currentUser.getId());
        if(sysUser==null){
            return new ReturnEntity(BaseConstant.FAIL,"用户信息错误");
        }
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getPrice()+"元/"+saleProduct.getUseDay()+"天试用期套餐]】需要"+saleProduct.getPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getOneYearPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getOneYearPrice()+"元/一年套餐]】需要"+saleProduct.getOneYearPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getTwoYearPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getTwoYearPrice()+"元/两年套餐]】需要"+saleProduct.getTwoYearPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else if(SaleBaseContent.SALE_PRICE_TYPE_SN.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getThreeYearPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getThreeYearPrice()+"元/三年套餐]】需要"+saleProduct.getThreeYearPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else {
            return new ReturnEntity(BaseConstant.FAIL,"套餐类型不存在！");

        }

        //创建我的商品及我的订单
        SaleProductMy saleProductMy=new SaleProductMy(sysUser);
        SaleDetail saleDetail=new SaleDetail(sysUser);
        saleDetail.setUserId(sysUser.getId());
        saleDetail.setProductId(saleProduct.getId());
        saleDetail.setProductMyId(saleProductMy.getId());
        saleProductMy.setName(saleProduct.getName());
        saleProductMy.setThumImage(saleProduct.getThumImage());
        saleProductMy.setUserId(sysUser.getId());
        saleProductMy.setProductId(saleProduct.getId());
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            saleDetail.setMoney(saleProduct.getPrice()+"");
            saleDetail.setEndTime(DateUtil.getDateOfDate(new Date(),-saleProduct.getUseDay()));
            saleDetail.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getPrice()+"元/"+saleProduct.getUseDay()+"天试用期套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getDateOfDate(new Date(),-saleProduct.getUseDay()));
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            saleDetail.setMoney(saleProduct.getOneYearPrice()+"");
            saleDetail.setEndTime(DateUtil.getYearOfDates(new Date(),-1));
            saleDetail.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getOneYearPrice()+"元/一年套餐]】");

            saleProductMy.setUseEndTime(DateUtil.getYearOfDates(new Date(),-1));
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            saleDetail.setMoney(saleProduct.getTwoYearPrice()+"");
            saleDetail.setEndTime(DateUtil.getYearOfDates(new Date(),-2));
            saleDetail.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getTwoYearPrice()+"元/两年套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getYearOfDates(new Date(),-2));
        }else{
            saleDetail.setMoney(saleProduct.getThreeYearPrice()+"");
            saleDetail.setEndTime(DateUtil.getYearOfDates(new Date(),-3));
            saleDetail.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getThreeYearPrice()+"元/三年套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getYearOfDates(new Date(),-3));
        }
        saleProductMy.setHomeUrl(saleProduct.getHomeUrl());
        saleProductMy.setUserUrl(saleProduct.getUserUrl());
        int saleMyId=saleProductMyService.insert(saleProductMy);
        if(saleMyId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的商品失败！");
        }
        int saleDetailId=saleDetailService.insert(saleDetail);
        if(saleDetailId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的订单失败！");
        }
        //创建我的消费流水
        SaleRecord saleRecord=new SaleRecord(sysUser);
        saleRecord.setUserId(sysUser.getId());
        saleRecord.setType(SaleBaseContent.SALE_RECORD_TYPE_XF);
        saleRecord.setExtendId(saleProductMy.getId());
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            saleRecord.setMoney(saleProduct.getPrice()+"");
            saleRecord.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getPrice()+"元/"+saleProduct.getUseDay()+"天试用期套餐]】，消费"+saleProduct.getPrice()+"元。");
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            saleRecord.setMoney(saleProduct.getOneYearPrice()+"");
            saleRecord.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getOneYearPrice()+"元/一年套餐]】，消费"+saleProduct.getOneYearPrice()+"元。");
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            saleRecord.setMoney(saleProduct.getTwoYearPrice()+"");
            saleRecord.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getTwoYearPrice()+"元/两年套餐]】，消费"+saleProduct.getTwoYearPrice()+"元。");
        }else{
            saleRecord.setMoney(saleProduct.getThreeYearPrice()+"");
            saleRecord.setRemarks("购买【"+saleProduct.getName()+"["+saleProduct.getThreeYearPrice()+"元/三年套餐]】，消费"+saleProduct.getThreeYearPrice()+"元。");
        }
        int saleRecordId=saleRecordService.insert(saleRecord);
        if(saleRecordId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的消费流水失败！");
        }
        //减去账号消费金额
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getPrice());
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getOneYearPrice());
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getTwoYearPrice());
        }else{
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getThreeYearPrice());
        }
        int usesId=sysUserService.updateByPrimaryKey(sysUser);
        if(usesId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"减去账户消费金额失败！");
        }else{
            request.getSession().setAttribute(BaseConstant.SESSION_CURRENT_USER,sysUser);
        }
        return new ReturnEntity();
    }
    /**
     * 续费产品
     *
     * @param id 商品id
     * @param productMyId 我的续费商品id
     * @param priceType 套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
     * @param currentUser 当前用户
     * @param request
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnEntity addProduct(HttpServletRequest request, String id, String priceType,String productMyId, SysUser currentUser) {
        SaleProduct saleProduct=selectByPrimaryKey(id);
        if(saleProduct==null){
            return new ReturnEntity(BaseConstant.FAIL,"id错误");
        }
        SysUser sysUser=sysUserService.selectByPrimaryKey(currentUser.getId());
        if(sysUser==null){
            return new ReturnEntity(BaseConstant.FAIL,"用户信息错误");
        }
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getPrice()+"元/"+saleProduct.getUseDay()+"天试用期套餐]】需要"+saleProduct.getPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getOneYearPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getOneYearPrice()+"元/一年套餐]】需要"+saleProduct.getOneYearPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getTwoYearPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getTwoYearPrice()+"元/两年套餐]】需要"+saleProduct.getTwoYearPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else if(SaleBaseContent.SALE_PRICE_TYPE_SN.equals(priceType)){
            if(sysUser.getMoney()<saleProduct.getThreeYearPrice()){
                return new ReturnEntity(BaseConstant.FAIL,"购买【"+saleProduct.getName()+"["+saleProduct.getThreeYearPrice()+"元/三年套餐]】需要"+saleProduct.getThreeYearPrice()+"元，您的账户只有"+sysUser.getMoney()+"元。请充值！");
            }
        }else {
            return new ReturnEntity(BaseConstant.FAIL,"套餐类型不存在！");

        }

        //创建我的商品及我的订单
        SaleProductMy saleProductMy=saleProductMyService.selectByPrimaryKey(productMyId);
        if(saleProductMy==null){
            return new ReturnEntity(BaseConstant.FAIL,"续费商品不存在！");
        }

        SaleDetail saleDetail=new SaleDetail(sysUser);
        saleDetail.setUserId(sysUser.getId());
        saleDetail.setProductId(saleProduct.getId());
        saleDetail.setProductMyId(saleProductMy.getId());

        Date currentDate=new Date();
        if(saleProductMy.getUseEndTime().getTime()>currentDate.getTime()){
            currentDate=saleProductMy.getUseEndTime();
        }
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            saleDetail.setMoney(saleProduct.getPrice()+"");
            saleDetail.setEndTime(DateUtil.getDateOfDate(currentDate,-saleProduct.getUseDay()));
            saleDetail.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getPrice()+"元/"+saleProduct.getUseDay()+"天试用期套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getDateOfDate(currentDate,-saleProduct.getUseDay()));
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            saleDetail.setMoney(saleProduct.getOneYearPrice()+"");
            saleDetail.setEndTime(DateUtil.getYearOfDates(currentDate,-1));
            saleDetail.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getOneYearPrice()+"元/一年套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getYearOfDates(currentDate,-1));
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            saleDetail.setMoney(saleProduct.getTwoYearPrice()+"");
            saleDetail.setEndTime(DateUtil.getYearOfDates(currentDate,-2));
            saleDetail.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getTwoYearPrice()+"元/两年套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getYearOfDates(currentDate,-2));
        }else{
            saleDetail.setMoney(saleProduct.getThreeYearPrice()+"");
            saleDetail.setEndTime(DateUtil.getYearOfDates(currentDate,-3));
            saleDetail.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getThreeYearPrice()+"元/三年套餐]】");
            saleProductMy.setUseEndTime(DateUtil.getYearOfDates(currentDate,-3));
        }

        int saleMyId=saleProductMyService.updateByPrimaryKey(saleProductMy);
        if(saleMyId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的商品失败！");
        }
        int saleDetailId=saleDetailService.insert(saleDetail);
        if(saleDetailId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的订单失败！");
        }
        //创建我的消费流水
        SaleRecord saleRecord=new SaleRecord(sysUser);
        saleRecord.setUserId(sysUser.getId());
        saleRecord.setType(SaleBaseContent.SALE_RECORD_TYPE_XF);

        saleRecord.setExtendId(saleProductMy.getId());
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            saleRecord.setMoney(saleProduct.getPrice()+"");
            saleRecord.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getPrice()+"元/"+saleProduct.getUseDay()+"天试用期套餐]】，消费"+saleProduct.getPrice()+"元。");
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            saleRecord.setMoney(saleProduct.getOneYearPrice()+"");
            saleRecord.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getOneYearPrice()+"元/一年套餐]】，消费"+saleProduct.getOneYearPrice()+"元。");
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            saleRecord.setMoney(saleProduct.getTwoYearPrice()+"");
            saleRecord.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getTwoYearPrice()+"元/两年套餐]】，消费"+saleProduct.getTwoYearPrice()+"元。");
        }else{
            saleRecord.setMoney(saleProduct.getThreeYearPrice()+"");
            saleRecord.setRemarks("您为"+saleProductMy.getName()+"商品续费，购买【"+saleProduct.getName()+"["+saleProduct.getThreeYearPrice()+"元/三年套餐]】，消费"+saleProduct.getThreeYearPrice()+"元。");
        }
        int saleRecordId=saleRecordService.insert(saleRecord);
        if(saleRecordId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的消费流水失败！");
        }
        //减去账号消费金额
        //套餐类型:1、试用期套餐，2、一年套餐，3、两年套餐，4、三年套餐
        if(SaleBaseContent.SALE_PRICE_TYPE_SYQ.equals(priceType)){
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getPrice());
        }else if(SaleBaseContent.SALE_PRICE_TYPE_YN.equals(priceType)){
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getOneYearPrice());
        }else if(SaleBaseContent.SALE_PRICE_TYPE_NN.equals(priceType)){
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getTwoYearPrice());
        }else{
            sysUser.setMoney(sysUser.getMoney()-saleProduct.getThreeYearPrice());
        }
        int usesId=sysUserService.updateByPrimaryKey(sysUser);
        if(usesId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"减去账户消费金额失败！");
        }else{
            request.getSession().setAttribute(BaseConstant.SESSION_CURRENT_USER,sysUser);
        }
        return new ReturnEntity();
    }
    /**
     * 分页查询
     *
     * @param saleProductVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    public ReturnEntity page(SaleProductVo saleProductVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SaleProduct> list = acceptList(saleProductVo);
        PageInfo<SaleProduct> page=new PageInfo<SaleProduct>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 列表查询
     *
     * @param saleProductVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    public ReturnEntity list(SaleProductVo saleProductVo) {
        PageHelper.orderBy(" update_time desc");
        List<SaleProduct> list = acceptList(saleProductVo);
        return new ReturnEntity(list);
    }

    /**
     * 修改
     *
     * @param saleProductVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    public ReturnEntity addOrUpdate(SaleProductVo saleProductVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(saleProductVo.getId())){
            saleProduct=new SaleProduct(currentUser);
            isUpate=false;
        }else{
            saleProduct=selectByPrimaryKey(saleProductVo.getId());
            if(ObjectUtil.isNull(saleProduct)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            saleProduct.setUpdateTime(new Date());
            saleProduct.setUpdateUser(currentUser.getId());
        }
        saleProduct.setName(saleProductVo.getName());
        saleProduct.setPrice(saleProductVo.getPrice());
        saleProduct.setOneYearPrice(saleProductVo.getOneYearPrice());
        saleProduct.setTwoYearPrice(saleProductVo.getTwoYearPrice());
        saleProduct.setThreeYearPrice(saleProductVo.getThreeYearPrice());
        saleProduct.setVersionRecord(saleProductVo.getVersionRecord());
        saleProduct.setVersionNum(saleProductVo.getVersionNum());
        saleProduct.setUseDay(saleProductVo.getUseDay());
        saleProduct.setUserUrl(saleProductVo.getUserUrl());
        saleProduct.setHomeUrl(saleProductVo.getHomeUrl());
        saleProduct.setColId(saleProductVo.getColId());
        saleProduct.setThumImage(saleProductVo.getThumImage());
        saleProduct.setContent(saleProductVo.getContent());
        saleProduct.setSort(saleProductVo.getSort());
        saleProduct.setShowType(saleProductVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(saleProduct);
        }else{
            num=insert(saleProduct);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SaleProduct saleProduct=selectByPrimaryKey(id);
        if(saleProduct!=null){
            saleProduct.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            saleProduct.setUpdateTime(new Date());
            saleProduct.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(saleProduct);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 获取组织机构和人员树
     *
     * @param saleProductVo 对象
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @Override
    public ReturnEntity dTreeList(SaleProductVo saleProductVo) {
        List<SaleProduct> saleProductList = acceptList(saleProductVo);
        List<SaleColumn> saleColumnList = saleColumnService.acceptList(new SaleColumnVo());
        List<Map<String, Object>> dtreeEntityList = new ArrayList<Map<String, Object>>();
        for (SaleColumn saleColumn : saleColumnList) {
            Map<String, Object> dtreeEntity = new HashedMap();
            dtreeEntity.put("id", saleColumn.getId());
            dtreeEntity.put("title", saleColumn.getName());
            dtreeEntity.put("parentId","0");
            dtreeEntity.put("isLast", false);
            Map<String, String> m = new HashedMap();
            m.put("type", "0");
            m.put("isChecked", "0");
            List<Map<String, String>> dtreeCheckEntityList = new ArrayList<Map<String, String>>();
            dtreeCheckEntityList.add(m);
            dtreeEntity.put("checkArr", dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        for (SaleProduct saleProduct : saleProductList) {
            Map<String, Object> dtreeEntity = new HashedMap();
            dtreeEntity.put("id", saleProduct.getId());
            dtreeEntity.put("title", saleProduct.getName());
            dtreeEntity.put("parentId", saleProduct.getColId());
            dtreeEntity.put("isLast", true);
            Map<String, String> m = new HashedMap();
            m.put("type", "0");
            m.put("isChecked", "0");
            List<Map<String, String>> dtreeCheckEntityList = new ArrayList<Map<String, String>>();
            dtreeCheckEntityList.add(m);
            dtreeEntity.put("checkArr", dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        return new ReturnEntity(dtreeEntityList);
    }
    /**
     * 获取集合
     *
     * @param saleProductVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    public List<SaleProduct> acceptList(SaleProductVo saleProductVo){
        Example example = new Example(SaleProduct.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(saleProductVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",saleProductVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(saleProductVo.getName())){
            criteria.andLike("name","%"+saleProductVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(saleProductVo.getColId())){
            criteria.andEqualTo("colId",saleProductVo.getColId());
        }
        example.setOrderByClause(" update_time desc");
        return saleProductDao.selectByExample(example);
    }
}
